package com.kymatrix.mpo.plus.bpm.server.controller.definition;

import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmProcessListenerEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.listener.PageQueryBpmProcessListenerReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.listener.QueryBpmProcessListenerResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.listener.SaveBpmProcessListenerReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.listener.UpdateBpmProcessListenerReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmProcessListenerService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.business.common.ResultResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "BPM 流程监听器相关请求处理类")
@RestController
@Validated
public class BpmProcessListenerController {

    @Resource
    private BpmProcessListenerService processListenerService;

    @PostMapping("/bpm/process-listener/create")
    @ApiOperation(value = "创建流程监听器")
    public ResultResp<String> createProcessListener(@Valid @RequestBody SaveBpmProcessListenerReq req) {
        return ResultResp.ok(processListenerService.createProcessListener(req));
    }

    @PutMapping("/bpm/process-listener/update")
    @ApiOperation(value = "更新流程监听器")
    public ResultResp<Boolean> updateProcessListener(@Valid @RequestBody UpdateBpmProcessListenerReq req) {
        processListenerService.updateProcessListener(req);
        return ResultResp.ok(true);
    }

    @DeleteMapping("/bpm/process-listener/delete")
    @ApiOperation(value = "删除流程监听器")
    public ResultResp<Boolean> deleteProcessListener(@RequestParam("id") String id) {
        processListenerService.deleteProcessListener(id);
        return ResultResp.ok(true);
    }

    @GetMapping("/bpm/process-listener/get")
    @ApiOperation(value = "获得流程监听器")
    public ResultResp<QueryBpmProcessListenerResp> getProcessListener(@RequestParam("id") String id) {
        BpmProcessListenerEntity processListener = processListenerService.getProcessListener(id);
        return ResultResp.ok(QueryBpmProcessListenerResp.fromEntity(processListener));
    }

    @GetMapping("/bpm/process-listener/page")
    @ApiOperation(value = "获得流程监听器分页")
    public ResultResp<PageResp<QueryBpmProcessListenerResp>> getProcessListenerPage(
            @Valid PageQueryBpmProcessListenerReq req) {
        PageResp<BpmProcessListenerEntity> pageResult = processListenerService.getProcessListenerPage(req);
        PageResp<QueryBpmProcessListenerResp> res = new PageResp<>();
        res.setTotal(pageResult.getTotal());
        for (BpmProcessListenerEntity entity : pageResult.getList()) {
            res.getList().add(QueryBpmProcessListenerResp.fromEntity(entity));
        }
        return ResultResp.ok(res);
    }

}
